﻿@using Nop.Services.Stores
@model ProductReviewSearchModel

@{
    //page title
    ViewBag.PageTitle = T("Admin.Catalog.ProductReviews").Text;
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("Product reviews");
}

@{
    const string hideSearchBlockAttributeName = "ProductReviewsPage.HideSearchBlock";
    var hideSearchBlock = genericAttributeService.GetAttribute<bool>(workContext.CurrentCustomer, hideSearchBlockAttributeName);
}
@inject IStoreService storeService

<div class="content-header clearfix">
    <h1 class="pull-left">
        @T("Admin.Catalog.ProductReviews")
    </h1>
    <div class="pull-right">
        @if (!Model.IsLoggedInAsVendor)
        {
            //these buttons are not visible to vendors
            <button type="button" id="approve-selected" class="btn bg-green">
                <i class="fa fa-check-square-o"></i>
                @T("Admin.Catalog.ProductReviews.ApproveSelected")
            </button>
            <button type="button" id="disapprove-selected" class="btn bg-navy">
                <i class="fa fa-minus-square-o"></i>
                @T("Admin.Catalog.ProductReviews.DisapproveSelected")
            </button>
            <button type="button" id="delete-selected" class="btn bg-red">
                <i class="fa fa-trash-o"></i>
                @T("Admin.Catalog.ProductReviews.DeleteSelected")
            </button>
            <nop-action-confirmation asp-button-id="delete-selected" />
        }
        @await Component.InvokeAsync("AdminWidget", new { widgetZone = AdminWidgetZones.ProductReviewListButtons })
    </div>
</div>

<div class="content">
    <div class="form-horizontal">
        <div class="panel-group">
            <div class="panel panel-default panel-search">
                <div class="panel-body">
                    <div class="row search-row @(!hideSearchBlock ? "opened" : "")" data-hideAttribute="@hideSearchBlockAttributeName">
                        <div class="search-text">@T("Admin.Common.Search")</div>
                        <div class="icon-search"><i class="fa fa-search" aria-hidden="true"></i></div>
                        <div class="icon-collapse"><i class="fa fa-angle-down" aria-hidden="true"></i></div>
                    </div>
                    <div class="search-body @(hideSearchBlock ? "closed" : "")">
                        <div class="row">
                            <div class="col-md-5">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="CreatedOnFrom" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="CreatedOnFrom" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="CreatedOnTo" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="CreatedOnTo" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchText" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="SearchText" />
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-7">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchApprovedId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchApprovedId" asp-items="Model.AvailableApprovedOptions" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchProductId" />
                                    </div>
                                    <div class="col-md-8">
                                        <input type="text" class="form-control" id="search-product-name" autocomplete="off" />
                                        <script>
                                        $(document).ready(function() {
                                            $('#search-product-name').autocomplete({
                                                delay: 500,
                                                minLength: 3,
                                                source: '@Url.Action("SearchAutoComplete", "SearchComplete")',
                                                select: function(event, ui) {
                                                    $('#@Html.IdFor(model => model.SearchProductId)')
                                                        .val(ui.item.productid);
                                                    $('#search-product-friendly-name').text(ui.item.label);
                                                    $('#search-product-clear').show();
                                                    return false;
                                                }
                                            });
                                        });
                                        </script>

                                        <span id="search-product-friendly-name"></span>
                                        <button type="button" id="search-product-clear" class="btn bg-gray" style="display: none; margin-top: 5px;">@T("Admin.Common.Clear")</button>
                                        <input asp-for="SearchProductId" autocomplete="off" style="display: none;" />

                                        <script>
                                        //remove button
                                        $('#search-product-clear').click(function() {
                                            $('#@Html.IdFor(model => model.SearchProductId)').val('0');
                                            $('#search-product-friendly-name').text('');
                                            $('#search-product-clear').hide();
                                            return false;
                                        });
                                        </script>
                                    </div>
                                </div>
                                <div class="form-group" @(Model.HideStoresList ? Html.Raw("style=\"display:none\"") : null)>
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchStoreId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchStoreId" asp-items="Model.AvailableStores" />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-7 col-md-offset-5">
                                <button type="button" id="search-productreviews" class="btn btn-primary btn-search">
                                    <i class="fa fa-search"></i>
                                    @T("Admin.Common.Search")
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-body">
                    @await Html.PartialAsync("Table", new DataTablesModel
                    {
                        Name = "productreviews-grid",
                        UrlRead = new DataUrl("List", "ProductReview", null),
                        SearchButtonId = "search-productreviews",
                        Length = Model.PageSize,
                        LengthMenu = Model.AvailablePageSizes,
                        Filters = new List<FilterParameter>
                        {
                            new FilterParameter(nameof(Model.CreatedOnFrom)),
                            new FilterParameter(nameof(Model.CreatedOnTo)),
                            new FilterParameter(nameof(Model.SearchText)),
                            new FilterParameter(nameof(Model.SearchStoreId)),
                            new FilterParameter(nameof(Model.SearchProductId)),
                            new FilterParameter(nameof(Model.SearchApprovedId))
                        },
                        ColumnCollection = new List<ColumnProperty>
                        {
                            new ColumnProperty(nameof(ProductReviewModel.Id))
                            {
                                IsMasterCheckBox = true,
                                Render = new RenderCheckBox("checkbox_product_reviews"),
                                Visible = !Model.IsLoggedInAsVendor,
                                ClassName =  NopColumnClassDefaults.CenterAll,
                                Width = "50"
                            },
                            new ColumnProperty(nameof(ProductReviewModel.StoreName))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.Store").Text,
                                Visible = storeService.GetAllStores().Count > 1 ? true : false,
                                Width = "150"
                            },
                            new ColumnProperty(nameof(ProductReviewModel.ProductName))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.Product").Text,
                                Width = "200",
                                Render = new RenderLink(new DataUrl("~/Admin/Product/Edit", nameof(ProductReviewModel.ProductId)))
                            },
                            new ColumnProperty(nameof(ProductReviewModel.CustomerInfo))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.Customer").Text,
                                Width = "200",
                                Render = new RenderLink(new DataUrl("~/Admin/Customer/Edit", nameof(ProductReviewModel.CustomerId)))
                            },
                            new ColumnProperty(nameof(ProductReviewModel.Title))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.Title").Text,
                                Width = "200"
                            },
                            new ColumnProperty(nameof(ProductReviewModel.ReviewText))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.ReviewText").Text,
                                Encode = false
                            },
                            new ColumnProperty(nameof(ProductReviewModel.ReplyText))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.ReplyText").Text,
                                Encode = false
                            },
                            new ColumnProperty(nameof(ProductReviewModel.Rating))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.Rating").Text,
                                Width = "50",
                                ClassName =  NopColumnClassDefaults.CenterAll
                            },
                            new ColumnProperty(nameof(ProductReviewModel.IsApproved))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.IsApproved").Text,
                                Width = "100",
                                ClassName =  NopColumnClassDefaults.CenterAll,
                                Render = new RenderBoolean()
                            },
                            new ColumnProperty(nameof(ProductReviewModel.CreatedOn))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.CreatedOn").Text,
                                Width = "150",
                                Render = new RenderDate()
                            },
                            new ColumnProperty(nameof(ProductReviewModel.Id))
                            {
                                Title = T("Admin.Common.Edit").Text,
                                Width = "100",
                                ClassName =  NopColumnClassDefaults.Button,
                                Render = new RenderButtonEdit(new DataUrl("Edit"))
                            }
                        }
                    })

                    <script>
                        $(document).ready(function() {

                            //"approve selected" button
                            $('#approve-selected').click(function(e) {
                                e.preventDefault();

                                var postData = {
                                    selectedIds: selectedIds
                                };
                                addAntiForgeryToken(postData);

                                $.ajax({
                                    cache: false,
                                    type: "POST",
                                    url: "@(Url.Action("ApproveSelected", "ProductReview"))",
                                    data: postData,
                                    traditional: true,
                                    error: function (jqXHR, textStatus, errorThrown) {
                                        $('#approveSelectedFailed-info').text(errorThrown);
                                        $("#approveSelectedFailed").click();
                                    },
                                    complete: function (jqXHR, textStatus) {
                                        updateTable('#productreviews-grid');
                                    }
                                });
                                return false;
                            });

                            //"disapprove selected" button
                            $('#disapprove-selected').click(function(e) {
                                e.preventDefault();

                                var postData = {
                                    selectedIds: selectedIds
                                };
                                addAntiForgeryToken(postData);

                                $.ajax({
                                    cache: false,
                                    type: "POST",
                                    url: "@(Url.Action("DisapproveSelected", "ProductReview"))",
                                    data: postData,
                                    traditional: true,
                                    error: function (jqXHR, textStatus, errorThrown) {
                                        $('#disapproveSelectedFailed-info').text(errorThrown);
                                        $("#disapproveSelectedFailed").click();
                                    },
                                    complete: function (jqXHR, textStatus) {
                                        updateTable('#productreviews-grid');
                                    }
                                });
                                return false;
                            });

                            //"delete selected" button
                            $("#delete-selected-action-confirmation-submit-button").bind("click", function () {
                                var postData = {
                                    selectedIds: selectedIds
                                };
                                addAntiForgeryToken(postData);
                                $.ajax({
                                    cache: false,
                                    type: "POST",
                                    url: "@(Url.Action("DeleteSelected", "ProductReview"))",
                                    data: postData,
                                    error: function (jqXHR, textStatus, errorThrown) {
                                        $('#deleteSelectedFailed-info').text(errorThrown);
                                        $("#deleteSelectedFailed").click();
                                    },
                                    complete: function (jqXHR, textStatus) {
                                        updateTable('#productreviews-grid');
                                    }
                                });
                                $('#delete-selected-action-confirmation').modal('toggle');
                                return false;
                            });
                        });
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<nop-alert asp-alert-id="approveSelectedFailed" />
<nop-alert asp-alert-id="disapproveSelectedFailed" />
<nop-alert asp-alert-id="deleteSelectedFailed" />
